package fr.mywiki.business.wiki;

import java.util.ArrayList;
import java.util.Iterator;

import fr.mywiki.business.utils.StringUtils;

/**
 * This class can, from a wiki code text, extract the different complete words
 * inside it. For the moment, this class simply extends StringTokenizer with a
 * different set of delimiters.
 * 
 * TODO Manage marks ex : a<del>b</del>c shound return one word abc
 * TODO : Take into account ponctuation...
 * 
 * @see java.util.StringTokenizer (quasi-simple copy-paste)
 */
public class WikiWordTokenizer {
	private WikiWordTokenizer(){}
	
	private Iterator iterator; 
	private int nbElements;
	
	public WikiWordTokenizer(String src) {
		init(src);
	}
	
	private void init(String source) {
		ArrayList words = new ArrayList();
		int pos = 0;
		StringBuffer word = new StringBuffer();
		
		while (pos < source.length()) {
			char ch = source.charAt(pos);
			if (StringUtils.isLetter(ch))
				word.append(ch);
			else {
				if (word.length() > 0)
					words.add(word);
				word = new StringBuffer();
			}
			pos++;
		}
		
		if (word.length() > 0)
			words.add(word);
		
		nbElements = words.size();
		iterator = words.iterator();
	}
	
	public boolean hasMoreWords() {
		return iterator.hasNext();
	}
	
	public String nextWord() {
		return ((StringBuffer) iterator.next()).toString();
	}
	
	public int countWords() {
		return nbElements;
	}
}
